package com.kch.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.pagehelper.PageHelper;
import com.kch.common.core.controller.BaseController;
import com.kch.system.domain.Activity;
import com.kch.system.domain.JoinUs;
import com.kch.system.domain.Meeting;
import com.kch.system.domain.SysBanner;
import com.kch.system.domain.SysBusiness;
import com.kch.system.domain.SysMemberVoice;
import com.kch.system.domain.SysUser;
import com.kch.system.domain.Train;
import com.kch.system.domain.UserCollect;
import com.kch.system.mapper.ActivityMapper;
import com.kch.system.mapper.JoinUsMapper;
import com.kch.system.mapper.MeetingMapper;
import com.kch.system.mapper.OtherConfigMapper;
import com.kch.system.mapper.SysBannerMapper;
import com.kch.system.mapper.SysBusinessMapper;
import com.kch.system.mapper.SysMemberVoiceMapper;
import com.kch.system.mapper.SysTagMapper;
import com.kch.system.mapper.TrainMapper;
import com.kch.system.mapper.UserCollectMapper;
import com.kch.system.service.ContentService;
import com.kch.system.service.EnterpriseService;
import com.kch.system.service.IMeetingApplicantsService;
import com.kch.system.service.ITrainApplicantsService;
import com.kch.system.service.IUserCollectService;
import com.kch.system.vo.BannerIndexVO;
import com.kch.system.vo.PortalReqVO;
import com.kch.system.vo.PortalVO;
import com.kch.system.vo.SysTagResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ContentServiceImpl extends BaseController implements ContentService {

    @Autowired
    private SysBusinessMapper sysBusinessMapper;
    @Autowired
    private MeetingMapper meetingMapper;
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private TrainMapper trainMapper;
    @Autowired
    private SysBannerMapper sysBannerMapper;
    @Autowired
    private OtherConfigMapper otherConfigMapper;
    @Autowired
    private SysMemberVoiceMapper sysMemberVoiceMapper;
    @Autowired
    private JoinUsMapper joinUsMapper;
    @Autowired
    private SysTagMapper sysTagMapper;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private IMeetingApplicantsService meetingApplicantsService;
    @Autowired
    private ITrainApplicantsService trainApplicantsService;
    @Autowired
    private UserCollectMapper userCollectMapper;

    @Override
    public PortalVO getDataByCode(PortalReqVO req, SysUser sysUser) {
        PortalVO portalVO = new PortalVO();
        JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
        switch (req.getCode()) {
            case "FA":
            case "XQ":
            case "ZJ":
            case "WT":
                boolean flag = false;
                if (!ObjectUtils.isEmpty(req.getPageNum()) && !ObjectUtils.isEmpty(req.getPageSize())) {
                    PageHelper.startPage(req.getPageNum(), req.getPageSize());
                    flag = true;
                }
                List<SysBusiness> sysBusinesses = sysBusinessMapper.selectByCode(req.getCode(), req.getTag());
                portalVO.setCode(req.getCode());
                portalVO.setContent(JSON.toJSONString(flag ? getDataTable(sysBusinesses) : sysBusinesses, SerializerFeature.WriteDateUseDateFormat));
                break;
            case "HY":
                flag = false;
                if (!ObjectUtils.isEmpty(req.getPageNum()) && !ObjectUtils.isEmpty(req.getPageSize())) {
                    PageHelper.startPage(req.getPageNum(), req.getPageSize());
                    flag = true;
                }
                List<Meeting> meetings = meetingMapper.selectByCode(req.getCode(), req.getTag());
                if(!CollectionUtils.isEmpty(meetings)){
                    if(ObjectUtils.isEmpty(sysUser)){
                        //当用户为为空，即没有登录
                        meetings.forEach(meeting -> meeting.setPassFlag("0"));
                    }else if(enterpriseService.enterPriseIsPass(sysUser.getUserId().intValue())){
                        meetings.forEach(meeting -> meeting.setPassFlag("1"));
                    }else{
                        List<Integer> meetingIds = new ArrayList<>();
                        meetings.forEach(meeting -> meetingIds.add(meeting.getId()));
                        Map<Integer, Integer> checkMap = meetingApplicantsService.checkUserReg(meetingIds, sysUser.getUserId().intValue());
                        meetings.forEach(meeting -> {
                            Integer userId = checkMap.get(meeting.getId());
                            if(ObjectUtils.isEmpty(userId)){
                                meeting.setPassFlag("2");
                            }else{
                                meeting.setPassFlag("3");
                            }
                        });
                    }
                }
                portalVO.setCode(req.getCode());
                portalVO.setContent(JSON.toJSONString(flag ? getDataTable(meetings) : meetings, SerializerFeature.WriteDateUseDateFormat));
                logger.info("编码" + req.getCode() + "返回参数" + portalVO.getContent());
                break;
            case "HD":
                flag = false;
                if (!ObjectUtils.isEmpty(req.getPageNum()) && !ObjectUtils.isEmpty(req.getPageSize())) {
                    PageHelper.startPage(req.getPageNum(), req.getPageSize());
                    flag = true;
                }
                List<Activity> activities = activityMapper.selectByCode(req.getCode(), req.getTag());
                portalVO.setCode(req.getCode());
                portalVO.setContent(JSON.toJSONString(flag ? getDataTable(activities) : activities, SerializerFeature.WriteDateUseDateFormat));
                break;
            case "PX":
                flag = false;
                if (!ObjectUtils.isEmpty(req.getPageNum()) && !ObjectUtils.isEmpty(req.getPageSize())) {
                    PageHelper.startPage(req.getPageNum(), req.getPageSize());
                    flag = true;
                }
                List<Train> trains = trainMapper.selectByCode(req.getCode());
                if(!CollectionUtils.isEmpty(trains)){
                    if(ObjectUtils.isEmpty(sysUser)){
                        //当用户为为空，即没有登录
                        trains.forEach(train -> train.setPassFlag("0"));
                    }else if(enterpriseService.enterPriseIsPass(sysUser.getUserId().intValue())){
                        trains.forEach(train -> train.setPassFlag("1"));
                    }else{
                        List<Integer> trainIds = new ArrayList<>();
                        trains.forEach(meeting -> trainIds.add(meeting.getId()));
                        Map<Integer, Integer> checkMap = trainApplicantsService.checkUserReg(trainIds, sysUser.getUserId().intValue());
                        trains.forEach(train -> {
                            Integer userId = checkMap.get(train.getId());
                            if(ObjectUtils.isEmpty(userId)){
                                train.setPassFlag("2");
                            }else{
                                train.setPassFlag("3");
                            }
                        });
                    }
                }
                portalVO.setCode(req.getCode());
                portalVO.setContent(JSON.toJSONString(flag ? getDataTable(trains) : trains, SerializerFeature.WriteDateUseDateFormat));
                break;
            case "DB":
            case "HB":
                List<SysBanner> sysBanners = sysBannerMapper.selectByCode(req.getCode());
                portalVO.setCode(req.getCode());
                portalVO.setContent(JSON.toJSONString(sysBanners, SerializerFeature.WriteDateUseDateFormat));
                break;
            case "JU":
                flag = false;
                if (!ObjectUtils.isEmpty(req.getPageNum()) && !ObjectUtils.isEmpty(req.getPageSize())) {
                    PageHelper.startPage(req.getPageNum(), req.getPageSize());
                    flag = true;
                }
                List<JoinUs> joinUses = joinUsMapper.selectListContent();
                portalVO.setCode(req.getCode());
                portalVO.setContent(JSON.toJSONString(flag ? getDataTable(joinUses) : joinUses, SerializerFeature.WriteDateUseDateFormat));
                break;
            case "HYS":
                flag = false;
                if (!ObjectUtils.isEmpty(req.getPageNum()) && !ObjectUtils.isEmpty(req.getPageSize())) {
                    PageHelper.startPage(req.getPageNum(), req.getPageSize());
                    flag = true;
                }
                List<SysMemberVoice> sysMemberVoices = sysMemberVoiceMapper.selectMemberList();
                portalVO.setCode(req.getCode());
                portalVO.setContent(JSON.toJSONString(flag ? getDataTable(sysMemberVoices) : sysMemberVoices, SerializerFeature.WriteDateUseDateFormat));
                break;
            default:
                List<String> configList = otherConfigMapper.selectByCode(req.getCode());
                portalVO.setCode(req.getCode());
                List<Object> list = new ArrayList<>();
                for (String config : configList) {
                    Object parse = JSON.parse(config);
                    list.add(parse);
                }
                portalVO.setContent(JSON.toJSONString(list));
                break;
        }
        return portalVO;
    }

    @Override
    public PortalVO getDataByCodeDetail(PortalReqVO req, SysUser sysUser) {
        if (ObjectUtils.isEmpty(req.getId())) {
            throw new RuntimeException("id不能为空");
        }
        PortalVO portalVO = new PortalVO();
        JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
        switch (req.getCode()) {
            case "FA":
            case "XQ":
            case "ZJ":
            case "WT":
                SysBusiness sysBusiness = sysBusinessMapper.selectByPrimaryKey(Integer.valueOf(req.getId()));
                if(!ObjectUtils.isEmpty(sysUser) && !ObjectUtils.isEmpty(sysBusiness)){
                    UserCollect collect = userCollectMapper.selectByUserAndBus(sysUser.getUserId().intValue(), sysBusiness.getId(), sysBusiness.getCloumnCode());
                    if(!ObjectUtils.isEmpty(collect)){
                        sysBusiness.setCollectFlag(true);
                    }else{
                        sysBusiness.setCollectFlag(false);
                    }
                }else{
                    sysBusiness.setCollectFlag(false);
                }
                portalVO.setCode(req.getCode());
                portalVO.setContent(JSON.toJSONString(sysBusiness, SerializerFeature.WriteDateUseDateFormat));
                break;
            case "HY":
                Meeting meetings = meetingMapper.selectByPrimaryKey(Integer.valueOf(req.getId()));
                if(!ObjectUtils.isEmpty(meetings)){
                    if(ObjectUtils.isEmpty(sysUser)){
                        //当用户为为空，即没有登录
                        meetings.setPassFlag("0");
                    }else if(enterpriseService.enterPriseIsPass(sysUser.getUserId().intValue())){
                        meetings.setPassFlag("1");
                    }else{
                        List<Integer> meetingIds = new ArrayList<>();
                        meetingIds.add(meetings.getId());
                        Map<Integer, Integer> checkMap = meetingApplicantsService.checkUserReg(meetingIds, sysUser.getUserId().intValue());
                        Integer userId = checkMap.get(meetings.getId());
                        if(ObjectUtils.isEmpty(userId)){
                            meetings.setPassFlag("2");
                        }else{
                            meetings.setPassFlag("3");
                        }
                    }
                }
                portalVO.setCode(req.getCode());
                portalVO.setContent(JSON.toJSONString(meetings, SerializerFeature.WriteDateUseDateFormat));
                break;
            case "HD":
                Activity activities = activityMapper.selectByPrimaryKey(Integer.valueOf(req.getId()));
                portalVO.setCode(req.getCode());
                portalVO.setContent(JSON.toJSONString(activities, SerializerFeature.WriteDateUseDateFormat));
                break;
            case "PX":
                Train trains = trainMapper.selectByPrimaryKey(Integer.valueOf(req.getId()));
                if(!ObjectUtils.isEmpty(trains)){
                    if(ObjectUtils.isEmpty(sysUser)){
                        //当用户为为空，即没有登录
                        trains.setPassFlag("0");
                    }else if(enterpriseService.enterPriseIsPass(sysUser.getUserId().intValue())){
                        trains.setPassFlag("1");
                    }else{
                        List<Integer> trainIds = new ArrayList<>();
                        trainIds.add(trains.getId());
                        Map<Integer, Integer> checkMap = trainApplicantsService.checkUserReg(trainIds, sysUser.getUserId().intValue());
                        Integer userId = checkMap.get(trains.getId());
                        if(ObjectUtils.isEmpty(userId)){
                            trains.setPassFlag("2");
                        }else{
                            trains.setPassFlag("3");
                        }
                    }
                }
                portalVO.setCode(req.getCode());
                portalVO.setContent(JSON.toJSONString(trains, SerializerFeature.WriteDateUseDateFormat));
                break;
            case "DB":
            case "HB":
                SysBanner sysBanners = sysBannerMapper.selectByPrimaryKey(Integer.valueOf(req.getId()));
                portalVO.setCode(req.getCode());
                portalVO.setContent(JSON.toJSONString(sysBanners, SerializerFeature.WriteDateUseDateFormat));
                break;
            case "JU":
                JoinUs joinUs = joinUsMapper.selectByPrimaryKey(Integer.valueOf(req.getId()));
                portalVO.setCode(req.getCode());
                portalVO.setContent(JSON.toJSONString(joinUs, SerializerFeature.WriteDateUseDateFormat));
                break;
            case "HYS":
                SysMemberVoice sysMemberVoice = sysMemberVoiceMapper.selectByPrimaryKey(Integer.valueOf(req.getId()));
                portalVO.setCode(req.getCode());
                portalVO.setContent(JSON.toJSONString(sysMemberVoice, SerializerFeature.WriteDateUseDateFormat));
                break;
            default:
                break;
        }
        return portalVO;
    }

    @Override
    public List<BannerIndexVO> getDataIndexCarousel() {
        return sysBusinessMapper.getDataIndexCarousel();
    }

    @Override
    public List<SysTagResp> getTagList(String code, Boolean type) {
        return sysTagMapper.selectTagByCode(code, type);
    }


}
